<?php

namespace app\admin\controller;

use think\Request;
use think\Db;
use think\Validate;

class User extends AdminController
{
    public function index()
    {


        //var_dump(Request()->action());die;
        $list = Db::name('user')
            ->field(['id', 'username', 'name'])
            ->select();
        foreach ($list as $v) {
            $role_ids = Db::name('user_role')->field('rid')->where(['uid' => $v['id']])->select();
            //定义空数组
            $roles = array();
            //遍历
            foreach ($role_ids as $value) {
                $roles[] = Db::name('role')->where(['id' => $value['rid'], 'status' => 1])->field('name')->find();
            }
            $v['role'] = $roles; //将新得到角色信息放置到$v中
            $arr[] = $v;
        }
//        echo db('table')->getlastsql();
        return view('user/index', [
            'list' => $arr
        ]);
    }

    public function add()
    {
        return view('admin@user/add');
    }

    public function doAdd(Request $request)
    {
        $data = input('post.');
        if ($data['userpass'] !== $data['reuserpass']) {
            $this->error('两次输入有误!');
        }

        unset($data['reuserpass']);
        $data['userpass'] = md5($data['userpass']);

        $result = Db::name('user')->insert($data);

        if ($result > 0) {
            return $this->success('添加成功!', '/admin/user/index');
        } else {
            return $this->error('添加失败!');
        }
    }

    public function rolelist($id)
    {
        // 判断是否传正确的值
        if (!preg_match("/^\d*$/", $id)) {
            return $this->success('404 NOT FOUND!', '/admin/User/index');
        }
        //查询角色信息
        $list = Db::name('role')->where(['status' => 1])->select();
        //查询当前用户信息
        $users = Db::name('user')->where(['id' => $id])->find();
        //获取当前用户的角色信息
        $rolelist = Db::name('user_role')->where(['uid' => $id])->select();

        $myrole = array(); //定义空数组
        //对用户的角色进行重组
        foreach ($rolelist as $v) {
            $myrole[] = $v['rid'];
        }
//        var_dump($myrole);die;

        // 渲染模板
        return view('user/rolelist', [
            'list' => $list,
            'users' => $users,
            'myrole' => $myrole
        ]);
    }

    public function edit()
    {

    }

    public function savenode(Request $request)
    {
        $p = input('post.');
        //判读必须选择一个角色
        if (empty($p['role'])) {
            $this->error("请选择一个角色！");
        }

        $uid = $p['uid'];
        // 判断是否存在当前用户
        $row = Db::name('user')->where(['id' => $uid])->find();
        if ($row == 0) {
            return $this->success('页面出错, 不要搞事情(╬◣д◢)', '/admin/User/index');
        }

        //清除用户所有的角色信息，避免重复添加
        Db::name('user_role')->where(['uid' => $uid])->delete();

        foreach ($p['role'] as $v) {
            $data['uid'] = $uid;
            $data['rid'] = $v;
            //执行添加
            Db::name('user_role')->insert($data);
        }

        return $this->success('角色分配成功', '/admin/User/index');
    }

}